package com.fm.app03.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fm.app03.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;

public class projectActivity extends AppCompatActivity {

    private List<TextView> textViewList = new ArrayList<>();
    private TextView mScoreTextView;
    private long mScore = 0;
    private Integer mScore2 = 0;
    private CountDownTimer mCountDownTimer;
    private List<String> stringList =  new ArrayList<>(Arrays.asList("付", "刘", "郑", "李"));
    private List<TextView> textViewList2 = new ArrayList<>();
    private Boolean IsClick = false;
    private long  millis;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_project);
        // 隐藏状态栏和导航栏
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_FULLSCREEN |
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_IMMERSIVE);
        // 获取到最外层的 LinearLayout
        LinearLayout rootLayout = findViewById(R.id.dadisuAll1);
        mScoreTextView = findViewById(R.id.fenshu1);
        // 为每个 ImageView 设置监听器
        setImageViewClickListener(rootLayout);
        // 设置开始按钮的点击事件
        Button startButton = findViewById(R.id.start_button1);
        startButton.setOnClickListener(v -> startGame());
        // 设置停止按钮的点击事件
        Button stopButton = findViewById(R.id.stop_button1);
        stopButton.setOnClickListener(v -> stopGame());
        startCountdown();
    }

    /*监听每个界面*/
    private void setImageViewClickListener(View view) {
        if (view instanceof TextView && !(view instanceof EditText) && !(view instanceof Button)) {
            TextView textView = (TextView) view;
            AtomicReference<String> randomElement = new AtomicReference<>(getRandomElement());
            ((TextView) view).setText(randomElement.get());
            textViewList.add(textView);
            textView.setOnClickListener(v -> {
                    v.setBackgroundResource(R.drawable.shouzhi);
                    textViewList2.add((TextView) v);
                    if(IsClick){
                        IsClick = false;
                        if (textViewList2.size() >= 2) {
                            TextView textView1 = textViewList2.get(0);
                            TextView textView2 = textViewList2.get(1);

                            String text1 = textView1.getText().toString();
                            String text2 = textView2.getText().toString();
                            if (text1.equals(text2) && textView1 != textView2) {
                                // 设置背景图
                                textView1.setBackgroundResource(R.color.bg);
                                textView1.setText("");
                                textView2.setBackgroundResource(R.color.bg);
                                textView2.setText("");

                                // 设置为不可点击
                                textView1.setClickable(false);
                                textView2.setClickable(false);
                                mScore+=2;
                                mScore2+=2;
                                mScoreTextView.setText("得分：" + mScore);
                            }else if (text1 == text2) {
                                // 设置背景图
                                textView1.setBackgroundResource(R.color.bg);
                                textView1.setText("");
                                // 设置为不可点击
                                textView1.setClickable(false);
                                mScore+=1;
                                mScore2+=1;
                                mScoreTextView.setText("得分：" + mScore);
                            }else {
                                textView1.setBackgroundResource(R.drawable.bg_1);
                                textView2.setBackgroundResource(R.drawable.bg_1);
                            }

                            /******/
                            textViewList2.clear();
                            Log.i("TAG", "setImageViewClickListener: "+mScore2+" "+textViewList.size());
                            if (mScore2.equals(textViewList.size())){
                                Log.i("TAG", "setImageViewClickListener: "+mScore2+" "+textViewList.size());
                            }

                        }
                    }
                    IsClick = true;
            });
        } else if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View child = viewGroup.getChildAt(i);
                setImageViewClickListener(child);
            }
        }
    }

    private void startGame() {
        // 初始化得分
        mScore = 0;
        mScoreTextView.setText("得分：" + mScore);
        //倒计时开始
        startCountdown();
        for (int i = 0; i <textViewList.size(); i++) {
            String randomElement = getRandomElement();
            textViewList.get(i).setText(randomElement);
            textViewList.get(i).setBackgroundResource(R.drawable.bg_1);
            textViewList.get(i).setClickable(true);
            textViewList.get(i).setClickable(true);
        }
    }
    private void stopGame() {
        // 取消倒计时任务
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }
    }
    private void startCountdown() {
        TextView timerView = findViewById(R.id.my_timer1);
        mCountDownTimer = new CountDownTimer(30000, 1000) { // 30 秒倒计时，每秒更新一次
            public void onTick(long millisUntilFinished) {
                timerView.setText("倒计时：" + millisUntilFinished / 1000);
                millis = millisUntilFinished / 1000;
            }
            public void onFinish() {
                timerView.setText("时间到！");
                stopGame();
            }
        }.start();
    }

    /*随机获取名字*/
    public String getRandomElement() {
        if (stringList.isEmpty()) {
            return null; // Handle the case where the list is empty
        } else {
            Random random = new Random();
            int randomIndex = random.nextInt(stringList.size());
            return stringList.get(randomIndex);
        }
    }
}